import datetime

from module_admin.dao.cms_order_approve_dao import CmsOrderApproveDao
from module_admin.dao.cms_order_dao import CmsOrderDao
from module_admin.entity.do.cms_order import CmsOrder
from module_admin.dao.dept_dao import *
from module_admin.entity.do.cms_order_approve import CmsOrderApprove
from module_admin.entity.vo.cms_order_approve_vo import OrderModelForApprove
from module_admin.entity.vo.user_vo import CasdoorUser
from module_admin.enum.state_enum import OrderStateEnum, OrderApproveStateEnum
from utils.uuid import UUID


class CmsOrderApproveService:

    @classmethod
    def pass_or_refuse(cls, db: Session, order_request: OrderModelForApprove, current_user: CasdoorUser):
        order_by_id: CmsOrder = CmsOrderDao.get_order_by_id(db, order_request.id)
        if order_by_id is None:
            return
        if order_by_id.state != OrderStateEnum.dsh.value:
            return
        cms_order_approve = CmsOrderApprove()
        uuid = UUID().uuid()
        cms_order_approve.id = int(uuid)
        cms_order_approve.order_id = order_by_id.order_id
        cms_order_approve.approve_id = "apr_" + uuid
        cms_order_approve.approve_time = datetime.datetime.now()
        cms_order_approve.create_by = current_user.name
        cms_order_approve.update_by = current_user.name
        state = order_by_id.state
        if order_request.result == 'pass':
            state = OrderStateEnum.ddd.value
            cms_order_approve.state = OrderApproveStateEnum.pass1.value
        elif order_request.result == 'refuse':
            state = OrderStateEnum.shbh.value
            cms_order_approve.state = OrderApproveStateEnum.refuse.value
        CmsOrderApproveDao.create_order_approve(db, cms_order_approve)
        CmsOrderDao.update_state(db, order_request.id, state)
